﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;

namespace LabMS.Web.DictionaryMaintenance
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class tree1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                LabMS.Model.DictionaryClass json ;
                LabMS.BLL.DictionaryClass bDictionaryclass = new LabMS.BLL.DictionaryClass();
                List<LabMS.Model.DictionaryClass> list = new List<LabMS.Model.DictionaryClass>();
                list = bDictionaryclass.GetModelList("1=1");

                if (list.Count == 0)
                {
                    //没记录
                    throw new Exception("字典表中没有记录！");
                }
                else
                {
                    List<LabMS.Model.DictionaryClass> list2 = new List<LabMS.Model.DictionaryClass>();
                    for (int i = 0; i < list.Count; i++)
                    {
                        json = new LabMS.Model.DictionaryClass();//避免值被覆盖
                        json.ID = list[i].ID;
                        json.Code = list[i].Code;
                        json.Name = list[i].Name;
                        list2.Add(json);
                    }

                    string strJson = Newtonsoft.Json.JavaScriptConvert.SerializeObject(list2);//序列化，把JSON转化为string类型

                    context.Response.ContentType = "application/json";
                    context.Response.Write(strJson);
                }
            }
            catch
            {
                context.Response.ContentType = "application/json";
                context.Response.Write("{error:true}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
